package io.renren.modules.web.token.service.impl;

import io.renren.common.utils.JsonUtils;
import io.renren.common.constant.Constants;
import io.renren.common.exception.RRException;
import io.renren.modules.token.request.InfoRequest;
import io.renren.modules.token.service.ApiTokenInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class ApiTokenInfoServiceImpl implements ApiTokenInfoService {


    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Map info(InfoRequest request) throws RRException {
        Map resultMap = new HashMap();
        //登录标识信息
        resultMap.put("sysUser", stringRedisTemplate.opsForValue().get(Constants.SYS_USER_ID + request.getUserId()));

        //角色信息
//        String sysRoleListStr = stringRedisTemplate.opsForValue().get(Constants.SYS_USER_ROLE + request.getUserId());
//        List list = new ArrayList();
//        List sysRoleList = (ArrayList) JsonUtils.valueAsObject(sysRoleListStr, ArrayList.class);
//        if (sysRoleList != null && sysRoleList.size() > 0) {
//            for (int i = 0; i < sysRoleList.size(); i++) {
//                Map role = (Map) sysRoleList.get(i);
//                list.add(role.get("name"));
//            }
//        }
//
//        resultMap.put("sysRoles", JsonUtils.valueAsString(list));
        resultMap.put("authzCodes", stringRedisTemplate.opsForValue().get(Constants.X_AUTHZ_CODE + request.getUserId()));

        //========================================
        //基础信息
        resultMap.put("cuInfo", stringRedisTemplate.opsForValue().get(Constants.CU_INFO + request.getUserId()));
        //统计信息
        resultMap.put("cuCount", stringRedisTemplate.opsForValue().get(Constants.CU_COUNT + request.getUserId()));
        //账号信息
        resultMap.put("cuAccount", stringRedisTemplate.opsForValue().get(Constants.CU_ACCOUNT + request.getUserId()));
        return resultMap;
    }

    @Override
    public Map sysUser(InfoRequest request) throws RRException {

        //登录标识信息
        String sysUser = stringRedisTemplate.opsForValue().get(Constants.SYS_USER_ID + request.getUserId());
        Map resultMap = (Map) JsonUtils.valueAsObject(sysUser, Map.class);
        return resultMap;
    }

    /***
     * 角色信息
     * @param request
     * @return
     */
    @Override
    public List sysRoles(InfoRequest request) throws RRException {

        //角色信息
//        String sysRoleListStr = stringRedisTemplate.opsForValue().get(Constants.SYS_USER_ROLE + request.getUserId());
        List list = new ArrayList();
//        List sysRoleList = (ArrayList) JsonUtils.valueAsObject(sysRoleListStr, ArrayList.class);
//        if (sysRoleList != null && sysRoleList.size() > 0) {
//            for (int i = 0; i < sysRoleList.size(); i++) {
//                Map role = (Map) sysRoleList.get(i);
//                list.add(role.get("name"));
//            }
//        }
        return list;
    }

    /***
     * 服务信息
     * @param request
     * @return
     */
    @Override
    public List authzCodes(InfoRequest request) throws RRException {

        String authzCodes = stringRedisTemplate.opsForValue().get(Constants.X_AUTHZ_CODE + request.getUserId());
        return (ArrayList) JsonUtils.valueAsObject(authzCodes, ArrayList.class);
    }

    /***
     * 基础信息
     * @param request
     * @return
     */
    @Override
    public Map cuInfo(InfoRequest request) throws RRException {
        //基础信息
        String cuInfo = stringRedisTemplate.opsForValue().get(Constants.CU_INFO + request.getUserId());
        return (Map) JsonUtils.valueAsObject(cuInfo, Map.class);
    }

    /***
     * 统计信息
     * @param request
     * @return
     */
    @Override
    public Map cuCount(InfoRequest request) throws RRException {
        //统计信息
        String cuCount = stringRedisTemplate.opsForValue().get(Constants.CU_COUNT + request.getUserId());
        return (Map) JsonUtils.valueAsObject(cuCount, Map.class);
    }

    /***
     * 账号信息
     * @param request
     * @return
     */
    @Override
    public Map cuAccount(InfoRequest request) throws RRException {
        //账号信息
        String cuAccount = stringRedisTemplate.opsForValue().get(Constants.CU_ACCOUNT + request.getUserId());

        return (Map) JsonUtils.valueAsObject(cuAccount, Map.class);
    }
}
